package ce51.dunghn.chatslide;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import vg.com.chatonline.activity.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.kdstudio.network.URLCachePolicy;
import com.kdstudio.network.URLRequest;
import com.kdstudio.network.URLRequestDelegate;
import com.kdstudio.network.reponse.URLBitmapResponse;
import com.kdstudio.network.reponse.URLJSONReponse;

public class SampleFragmentRight extends Fragment {
	private Handler mhHandler;
	String url = "http://android.dzanchoi.vn/api/images/list.xmob?id=17";
	SampleAdapter adapter;
	ListView lv;
	View v;

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.activity_contact, null);
	}

	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mhHandler = new Handler();
		adapter = new SampleAdapter(getActivity());
		v = (View) getView().findViewById(R.id.layout_contact);
		lv = (ListView) v.findViewById(R.id.contact_lvContact);
		lv.setAdapter(adapter);

		URLRequest request = new URLRequest(url, new RequestDelegate());
		// request.setReponse(new URLJSONArrayResponse());
		request.setCachePolicy(URLCachePolicy.MEMCACHE);
		request.send();
	}

	private class SampleItem {
		private String status;
		private String url_img;
		private String name;
		private Bitmap image;

		public String getUrl_img() {
			return url_img;
		}

		public void setUrl_img(String url_img) {
			this.url_img = url_img;
		}

		public SampleItem(String status, String name, String url_img) {

			this.status = status;
			this.url_img = url_img;
			this.name = name;

		}

		public String getStatus() {
			return status;
		}

		public void setStatus(String status) {
			this.status = status;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public Bitmap getImage() {
			return image;
		}

		public void setImage(Bitmap image) {
			this.image = image;
		}

		public void loadImage() {
			URLRequest request = new URLRequest(getUrl_img(),
					new RequestDelegate());
			request.setReponse(new URLBitmapResponse());
			request.setCachePolicy(URLCachePolicy.MEMCACHE);
			request.send();
		}

		private class RequestDelegate extends URLRequestDelegate {
			@Override
			public void onRequestReponse(final URLRequest httpRequest) {
				mhHandler.post(new Runnable() {

					public void run() {
						// TODO Auto-generated method stub
						URLBitmapResponse reponse = (URLBitmapResponse) httpRequest
								.getReponse();
						setImage(reponse.bitmap);
						adapter.notifyDataSetChanged();

					}
				});
				// TODO Auto-generated method stub

			}

			// anh hien thi khi timeout
			@Override
			public void onRequestError(URLRequest httpRequest, Throwable error) {
				// TODO Auto-generated method stub

			}

			// chay tu dau la anh gi khi load xong se thay doi
			@Override
			public void onRequestStart(URLRequest httpRequest) {
				// TODO Auto-generated method stub
				super.onRequestStart(httpRequest);
			}
		}

	}

	public class SampleAdapter extends ArrayAdapter<SampleItem> {

		private ImageView icon;
		private Button bt_name;
		private TextView tv_status;

		public SampleAdapter(Context context) {
			super(context, 0);
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getContext()).inflate(
						R.layout.item_contact, null);
			}
			icon = (ImageView) convertView
					.findViewById(R.id.contact_img_user_1);
			if (getItem(position).getImage() != null) {
				icon.setImageBitmap(getItem(position).getImage());
			} else {
				icon.setImageResource(R.drawable.ic_launcher);
				getItem(position).loadImage();
			}
			bt_name = (Button) convertView.findViewById(R.id.contact_tv_name_1);
			tv_status = (TextView) convertView
					.findViewById(R.id.contact_tv_status_1);

			bt_name.setText(getItem(position).name);
			tv_status.setText(getItem(position).status);

			return convertView;
		}

	}

	private class RequestDelegate extends URLRequestDelegate {
		@Override
		public void onRequestReponse(final URLRequest httpRequest) {
			mhHandler.post(new Runnable() {

				public void run() {
					// TODO Auto-generated method stub
					// URLBitmapResponse
					// reponse=(URLBitmapResponse)httpRequest.getReponse();
					// img.setImageBitmap(reponse.bitmap);

					URLJSONReponse response = (URLJSONReponse) httpRequest
							.getReponse();
					try {
						JSONArray jarr = response.jsonObj.getJSONArray("data");
						for (int i = 0; i < jarr.length(); i++) {
							JSONObject obj = jarr.getJSONObject(i);
							String name_test = obj.getString("name");
							String Status_test = obj.getString("body");
							String img = obj.getString("images");
							adapter.add(new SampleItem(name_test, Status_test,
									img));
						}
						adapter.notifyDataSetChanged();
						// tv.setText(response.jsonObj.toString());
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}
			});
			// TODO Auto-generated method stub

		}

		// anh hien thi khi timeout
		@Override
		public void onRequestError(URLRequest httpRequest, Throwable error) {
			// TODO Auto-generated method stub

		}

		// chay tu dau la anh gi khi load xong se thay doi
		@Override
		public void onRequestStart(URLRequest httpRequest) {
			// TODO Auto-generated method stub
			super.onRequestStart(httpRequest);
		}
	}
}
